package com.bytedance.common.plugin.component;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.bytedance.common.plugin.a;
import com.bytedance.common.plugin.component.ComponentHelper.COMPONENT_TYPE;
import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.reflect.Reflect;

public class PluginInstrumentation extends Instrumentation {
    private Instrumentation mBase;
    private Reflect mBaseRef;

    public PluginInstrumentation(Instrumentation arg2) {
        this.mBase = arg2;
        this.mBaseRef = Reflect.on(arg2);
    }

    public Instrumentation.ActivityMonitor addMonitor(IntentFilter arg2, Instrumentation.ActivityResult arg3, boolean arg4) {
        this.tryLoadPlugin();
        Instrumentation.ActivityMonitor v0 = this.mBase != null ? this.mBase.addMonitor(arg2, arg3, arg4) : super.addMonitor(arg2, arg3, arg4);
        return v0;
    }

    public Instrumentation.ActivityMonitor addMonitor(String arg2, Instrumentation.ActivityResult arg3, boolean arg4) {
        this.tryLoadPlugin();
        Instrumentation.ActivityMonitor v0 = this.mBase != null ? this.mBase.addMonitor(arg2, arg3, arg4) : super.addMonitor(arg2, arg3, arg4);
        return v0;
    }

    public void addMonitor(Instrumentation.ActivityMonitor arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.addMonitor(arg2);
        } else {
            super.addMonitor(arg2);
        }
    }

    public void callActivityOnCreate(Activity arg4, Bundle arg5) {
        this.tryLoadPlugin();
        try {
            if (this.mBase != null) {
                this.mBase.callActivityOnCreate(arg4, arg5);
            } else {
                super.callActivityOnCreate(arg4, arg5);
            }

            Logger.d("Plugin", arg4.getLocalClassName() + " callActivityOnCreate success");
        } catch (Throwable v0) {
            Logger.d("Plugin", arg4.getLocalClassName() + " callActivityOnCreate fail");
            try {
                this.injectResources(arg4, false);
                if (this.mBase != null) {
                    this.mBase.callActivityOnCreate(arg4, arg5);
                } else {
                    super.callActivityOnCreate(arg4, arg5);
                }

                Logger.d("Plugin", arg4.getLocalClassName() + " hack plugin resources callActivityOnCreate success");
            } catch (Throwable v1) {
                Logger.d("Plugin", arg4.getLocalClassName() + " hack plugin resources callActivityOnCreate fail");
                this.injectResources(arg4, true);
                if (this.mBase != null) {
                    this.mBase.callActivityOnCreate(arg4, arg5);
                } else {
                    super.callActivityOnCreate(arg4, arg5);
                }

                Logger.d("Plugin", arg4.getLocalClassName() + " hack origin resources callActivityOnCreate success");
            }
        }
    }

    public void callActivityOnDestroy(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnDestroy(arg2);
        } else {
            super.callActivityOnDestroy(arg2);
        }
    }

    public void callActivityOnNewIntent(Activity arg2, Intent arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnNewIntent(arg2, arg3);
        } else {
            super.callActivityOnNewIntent(arg2, arg3);
        }
    }

    public void callActivityOnPause(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnPause(arg2);
        } else {
            super.callActivityOnPause(arg2);
        }
    }

    public void callActivityOnPostCreate(Activity arg2, Bundle arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnPostCreate(arg2, arg3);
        } else {
            super.callActivityOnPostCreate(arg2, arg3);
        }
    }

    public void callActivityOnRestart(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnRestart(arg2);
        } else {
            super.callActivityOnRestart(arg2);
        }
    }

    public void callActivityOnRestoreInstanceState(Activity arg2, Bundle arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnRestoreInstanceState(arg2, arg3);
        } else {
            super.callActivityOnRestoreInstanceState(arg2, arg3);
        }
    }

    public void callActivityOnResume(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnResume(arg2);
        } else {
            super.callActivityOnResume(arg2);
        }
    }

    public void callActivityOnSaveInstanceState(Activity arg2, Bundle arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnSaveInstanceState(arg2, arg3);
        } else {
            super.callActivityOnSaveInstanceState(arg2, arg3);
        }
    }

    public void callActivityOnStart(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnStart(arg2);
        } else {
            super.callActivityOnStart(arg2);
        }
    }

    public void callActivityOnStop(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnStop(arg2);
        } else {
            super.callActivityOnStop(arg2);
        }
    }

    public void callActivityOnUserLeaving(Activity arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callActivityOnUserLeaving(arg2);
        } else {
            super.callActivityOnUserLeaving(arg2);
        }
    }

    public void callApplicationOnCreate(Application arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.callApplicationOnCreate(arg2);
        } else {
            super.callApplicationOnCreate(arg2);
        }
    }

    public boolean checkMonitorHit(Instrumentation.ActivityMonitor arg2, int arg3) {
        this.tryLoadPlugin();
        boolean v0 = this.mBase != null ? this.mBase.checkMonitorHit(arg2, arg3) : super.checkMonitorHit(arg2, arg3);
        return v0;
    }

    public void endPerformanceSnapshot() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.endPerformanceSnapshot();
        } else {
            super.endPerformanceSnapshot();
        }
    }

    public Instrumentation.ActivityResult execStartActivity(Context arg7, IBinder arg8, IBinder arg9, Activity arg10, Intent arg11, int arg12) {
        Object v0_2 = null;
        try {
            if (this.mBaseRef != null) {
                Intent[] v0_1 = ComponentHelper.tryConvertPlugin2Proxy(arg7, COMPONENT_TYPE.ACTIVITY, new Intent[]{arg11});
                if (v0_1 != null && v0_1.length > 0) {
                    arg11 = v0_1[0];
                }

                v0_2 = this.mBaseRef.call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12)}).target;
//                goto label_52;
            } else {
                v0_2 = Reflect.on(this).call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12)}).target;
            }
        } catch (Throwable v0) {
            Instrumentation.ActivityResult v0_3 = null;
        }

//        label_52:
        return ((Instrumentation.ActivityResult) v0_2);
    }

    public Instrumentation.ActivityResult execStartActivity(Context arg7, IBinder arg8, IBinder arg9, Activity arg10, Intent arg11, int arg12, Bundle arg13) {
        Object v0_2 = null;
        try {
            if (this.mBaseRef != null) {
                Intent[] v0_1 = ComponentHelper.tryConvertPlugin2Proxy(arg7, COMPONENT_TYPE.ACTIVITY, new Intent[]{arg11});
                if (v0_1 != null && v0_1.length > 0) {
                    arg11 = v0_1[0];
                }
                v0_2 = this.mBaseRef.call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE, Bundle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13}).target;
            } else {
                v0_2 = Reflect.on(this).call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE, Bundle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13}).target;
            }
        } catch (Throwable v0) {
        }

        return (ActivityResult) v0_2;
    }

    @TargetApi(value = 17)
    public Instrumentation.ActivityResult execStartActivity(Context arg7, IBinder arg8, IBinder arg9, Activity arg10, Intent arg11, int arg12, Bundle arg13, UserHandle arg14) {
        Object v0_2 = null;
        try {
            if (this.mBaseRef != null) {
                Intent[] v0_1 = ComponentHelper.tryConvertPlugin2Proxy(arg7, COMPONENT_TYPE.ACTIVITY, new Intent[]{arg11});
                if (v0_1 != null && v0_1.length > 0) {
                    arg11 = v0_1[0];
                }

                v0_2 = this.mBaseRef.call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE, Bundle.class, UserHandle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13, arg14}).target;
//                goto label_62;
            } else {
                v0_2 = Reflect.on(this).call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Activity.class, Intent.class, Integer.TYPE, Bundle.class, UserHandle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13, arg14}).target;
            }
        } catch (Throwable v0) {
        }

        label_62:
        return ((Instrumentation.ActivityResult) v0_2);
    }

    @TargetApi(value = 11)
    public Instrumentation.ActivityResult execStartActivity(Context arg7, IBinder arg8, IBinder arg9, Fragment arg10, Intent arg11, int arg12) {
        Object v0_2 = null;
        try {
            if (this.mBaseRef != null) {
                Intent[] v0_1 = ComponentHelper.tryConvertPlugin2Proxy(arg7, COMPONENT_TYPE.ACTIVITY, new Intent[]{arg11});
                if (v0_1 != null && v0_1.length > 0) {
                    arg11 = v0_1[0];
                }

                v0_2 = this.mBaseRef.call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Fragment.class, Intent.class, Integer.TYPE}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12)}).target;
//                goto label_52;
            } else {
                v0_2 = Reflect.on(this).call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Fragment.class, Intent.class, Integer.TYPE}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12)}).target;
            }
        } catch (Throwable v0) {
        }

//        label_52:
        return ((Instrumentation.ActivityResult) v0_2);
    }

    @TargetApi(value = 11)
    public Instrumentation.ActivityResult execStartActivity(Context arg7, IBinder arg8, IBinder arg9, Fragment arg10, Intent arg11, int arg12, Bundle arg13) {
        Object v0_2 = null;
        try {
            if (this.mBaseRef != null) {
                Intent[] v0_1 = ComponentHelper.tryConvertPlugin2Proxy(arg7, COMPONENT_TYPE.ACTIVITY, new Intent[]{arg11});
                if (v0_1 != null && v0_1.length > 0) {
                    arg11 = v0_1[0];
                }

                v0_2 = this.mBaseRef.call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Fragment.class, Intent.class, Integer.TYPE, Bundle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13}).target;
//                goto label_57;
            } else {
                v0_2 = Reflect.on(this).call("execStartActivity", new Class[]{Context.class, IBinder.class, IBinder.class, Fragment.class, Intent.class, Integer.TYPE, Bundle.class}, new Object[]{arg7, arg8, arg9, arg10, arg11, Integer.valueOf(arg12), arg13}).target;
            }
        } catch (Throwable v0) {
        }

        label_57:
        return ((Instrumentation.ActivityResult) v0_2);
    }

    public void finish(int arg2, Bundle arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.finish(arg2, arg3);
        } else {
            super.finish(arg2, arg3);
        }
    }

    public Bundle getAllocCounts() {
        this.tryLoadPlugin();
        Bundle v0 = this.mBase != null ? this.mBase.getAllocCounts() : super.getAllocCounts();
        return v0;
    }

    public Bundle getBinderCounts() {
        this.tryLoadPlugin();
        Bundle v0 = this.mBase != null ? this.mBase.getBinderCounts() : super.getBinderCounts();
        return v0;
    }

    public ComponentName getComponentName() {
        this.tryLoadPlugin();
        ComponentName v0 = this.mBase != null ? this.mBase.getComponentName() : super.getComponentName();
        return v0;
    }

    public Context getContext() {
        this.tryLoadPlugin();
        Context v0 = this.mBase != null ? this.mBase.getContext() : super.getContext();
        return v0;
    }

    public Context getTargetContext() {
        this.tryLoadPlugin();
        Context v0 = this.mBase != null ? this.mBase.getTargetContext() : super.getTargetContext();
        return v0;
    }

    @TargetApi(value = 18)
    public UiAutomation getUiAutomation() {
        this.tryLoadPlugin();
        UiAutomation v0 = this.mBase != null ? this.mBase.getUiAutomation() : super.getUiAutomation();
        return v0;
    }

    private void hookInjectResource(String arg5, Activity arg6) {
        if (!TextUtils.isEmpty(((CharSequence) arg5))) {
            if (!arg5.startsWith("com.ss.android.plugin") && !a.a.containsKey(arg5)) {
                Log.i("Plugin", String.format("The Actvity[%s] is no in HookMangager", arg5));
            } else {
                this.injectResources(arg6, false);
            }
        } else {
            label_11:
            Log.i("Plugin", String.format("The Actvity[%s] is no in HookMangager", arg5));
        }
    }

    private void injectResources(Activity arg8, boolean arg9) {
        Resources v0 = arg9 ? PluginManager.getPluginManager().d : PluginManager.getPluginManager().mResources;
        try {
            Reflect v1 = Reflect.on(arg8);
            v1.set("mResources", v0);
            try {
                if (arg8.getBaseContext() == null) {
//                    goto label_14;
                } else {
                    v1.set("mBase", new PluginContextImpl(arg8.getBaseContext(), arg9));
                }
            } catch (Throwable v1_1) {
            }
            Log.i("Plugin", String.format("Inject successfully.[Activity=%s].[res=%s]", arg8.getClass().getName(), v0.toString()));
        } catch (Exception v0_1) {
            label_36:
            Log.i("Plugin", String.format("Inject ContextThemeWapper error."));
        }
    }

    public boolean invokeContextMenuAction(Activity arg2, int arg3, int arg4) {
        this.tryLoadPlugin();
        boolean v0 = this.mBase != null ? this.mBase.invokeContextMenuAction(arg2, arg3, arg4) : super.invokeContextMenuAction(arg2, arg3, arg4);
        return v0;
    }

    public boolean invokeMenuActionSync(Activity arg2, int arg3, int arg4) {
        this.tryLoadPlugin();
        boolean v0 = this.mBase != null ? this.mBase.invokeMenuActionSync(arg2, arg3, arg4) : super.invokeMenuActionSync(arg2, arg3, arg4);
        return v0;
    }

    public boolean isProfiling() {
        this.tryLoadPlugin();
        boolean v0 = this.mBase != null ? this.mBase.isProfiling() : super.isProfiling();
        return v0;
    }

    public Activity newActivity(Class arg12, Context arg13, IBinder arg14, Application arg15, Intent arg16, ActivityInfo arg17, CharSequence arg18, Activity arg19, String arg20, Object arg21) throws InstantiationException, IllegalAccessException {
        Activity v0;
        this.tryLoadPlugin();
        if (this.mBase != null) {
            v0 = this.mBase.newActivity(arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21);
            this.hookInjectResource(arg12.getCanonicalName(), v0);
        } else {
            v0 = super.newActivity(arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21);
            this.hookInjectResource(arg12.getCanonicalName(), v0);
        }

        return v0;
    }

    public Activity newActivity(ClassLoader arg2, String arg3, Intent arg4) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Activity v0;
        this.tryLoadPlugin();
        if (this.mBase != null) {
            v0 = this.mBase.newActivity(arg2, arg3, arg4);
            this.hookInjectResource(arg3, v0);
        } else {
            v0 = super.newActivity(arg2, arg3, arg4);
            this.hookInjectResource(arg3, v0);
        }

        return v0;
    }

    public Application newApplication(ClassLoader arg2, String arg3, Context arg4) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        this.tryLoadPlugin();
        Application v0 = this.mBase != null ? this.mBase.newApplication(arg2, arg3, arg4) : super.newApplication(arg2, arg3, arg4);
        return v0;
    }

    public void onCreate(Bundle arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.onCreate(arg2);
        } else {
            super.onCreate(arg2);
        }
    }

    public void onDestroy() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.onDestroy();
        } else {
            super.onDestroy();
        }
    }

    public boolean onException(Object arg2, Throwable arg3) {
        this.tryLoadPlugin();
        boolean v0 = this.mBase != null ? this.mBase.onException(arg2, arg3) : super.onException(arg2, arg3);
        return v0;
    }

    public void onStart() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.onStart();
        } else {
            super.onStart();
        }
    }

    public void removeMonitor(Instrumentation.ActivityMonitor arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.removeMonitor(arg2);
        } else {
            super.removeMonitor(arg2);
        }
    }

    public void runOnMainSync(Runnable arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.runOnMainSync(arg2);
        } else {
            super.runOnMainSync(arg2);
        }
    }

    public void sendCharacterSync(int arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendCharacterSync(arg2);
        } else {
            super.sendCharacterSync(arg2);
        }
    }

    public void sendKeyDownUpSync(int arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendKeyDownUpSync(arg2);
        } else {
            super.sendKeyDownUpSync(arg2);
        }
    }

    public void sendKeySync(KeyEvent arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendKeySync(arg2);
        } else {
            super.sendKeySync(arg2);
        }
    }

    public void sendPointerSync(MotionEvent arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendPointerSync(arg2);
        } else {
            super.sendPointerSync(arg2);
        }
    }

    public void sendStatus(int arg2, Bundle arg3) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendStatus(arg2, arg3);
        } else {
            super.sendStatus(arg2, arg3);
        }
    }

    public void sendStringSync(String arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendStringSync(arg2);
        } else {
            super.sendStringSync(arg2);
        }
    }

    public void sendTrackballEventSync(MotionEvent arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.sendTrackballEventSync(arg2);
        } else {
            super.sendTrackballEventSync(arg2);
        }
    }

    public void setAutomaticPerformanceSnapshots() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.setAutomaticPerformanceSnapshots();
        } else {
            super.setAutomaticPerformanceSnapshots();
        }
    }

    public void setInTouchMode(boolean arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.setInTouchMode(arg2);
        } else {
            super.setInTouchMode(arg2);
        }
    }

    public void start() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.start();
        } else {
            super.start();
        }
    }

    public Activity startActivitySync(Intent arg2) {
        this.tryLoadPlugin();
        Activity v0 = this.mBase != null ? this.mBase.startActivitySync(arg2) : super.startActivitySync(arg2);
        return v0;
    }

    public void startAllocCounting() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.startAllocCounting();
        } else {
            super.startAllocCounting();
        }
    }

    public void startPerformanceSnapshot() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.startPerformanceSnapshot();
        } else {
            super.startPerformanceSnapshot();
        }
    }

    public void startProfiling() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.startProfiling();
        } else {
            super.startProfiling();
        }
    }

    public void stopAllocCounting() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.stopAllocCounting();
        } else {
            super.stopAllocCounting();
        }
    }

    public void stopProfiling() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.stopProfiling();
        } else {
            super.stopProfiling();
        }
    }

    private void tryLoadPlugin() {
    }

    public void waitForIdle(Runnable arg2) {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.waitForIdle(arg2);
        } else {
            super.waitForIdle(arg2);
        }
    }

    public void waitForIdleSync() {
        this.tryLoadPlugin();
        if (this.mBase != null) {
            this.mBase.waitForIdleSync();
        } else {
            super.waitForIdleSync();
        }
    }

    public Activity waitForMonitor(Instrumentation.ActivityMonitor arg2) {
        this.tryLoadPlugin();
        Activity v0 = this.mBase != null ? this.mBase.waitForMonitor(arg2) : super.waitForMonitor(arg2);
        return v0;
    }

    public Activity waitForMonitorWithTimeout(Instrumentation.ActivityMonitor arg3, long arg4) {
        this.tryLoadPlugin();
        Activity v0 = this.mBase != null ? this.mBase.waitForMonitorWithTimeout(arg3, arg4) : super.waitForMonitorWithTimeout(arg3, arg4);
        return v0;
    }
}

